package com.lkd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkd.entity.*;
import com.lkd.mapper.*;
import com.lkd.service.NodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkd.vo.NodeVo;
import com.lkd.vo.PageBean;
import com.lkd.vo.RegionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 点位表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-20
 */
@Service
public class NodeServiceImpl extends ServiceImpl<NodeMapper, Node> implements NodeService {


    @Autowired
    private RegionMapper regionMapper;

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Override
    public PageBean<NodeVo> search(String regionId,String name, Integer pageIndex, Integer pageSize) {
        Page<Node> page = new Page<>(pageIndex, pageSize);

        //根据区域id条件查询
        LambdaQueryWrapper<Node> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BeanUtil.isNotEmpty(regionId),Node::getRegionId, regionId)
                .like(name != null, Node::getName, name);
        page = getBaseMapper().selectPage(page, wrapper);

        List<NodeVo> nodeVoList = page.getRecords().stream().map(node -> {
            NodeVo nodeVo = BeanUtil.copyProperties(node, NodeVo.class);

            LambdaQueryWrapper<VendingMachine> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(VendingMachine::getNodeId, nodeVo.getId());
            List<VendingMachine> vendingMachineList = vendingMachineMapper.selectList(wrapper1);
            nodeVo.setVmCount(vendingMachineList.size());

            //获取region信息
            LambdaQueryWrapper<Region> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(Region::getId, nodeVo.getRegionId());
            Region region = regionMapper.selectOne(wrapper2);
            //复制给regionvo
            RegionVo regionVo = BeanUtil.copyProperties(region, RegionVo.class);
            //多表查询出nodecount
            LambdaQueryWrapper<Node> wrapper3 = new LambdaQueryWrapper<>();
            wrapper3.eq(Node::getRegionId, regionVo.getId());
            List<Node> nodeList = getBaseMapper().selectList(wrapper3);
            //把nodecount保存到regionvo
            regionVo.setNodeCount(nodeList.size());
            //把regionvo保存到nodevo
            nodeVo.setRegion(regionVo);

            //多表查询出business
            LambdaQueryWrapper<Business> wrapper4 = new LambdaQueryWrapper<>();
            wrapper4.eq(Business::getId, nodeVo.getBusinessId());
            //把business保存到nodevo
            nodeVo.setBusinessType(businessMapper.selectOne(wrapper4));


            return nodeVo;
        }).collect(Collectors.toList());


        return new PageBean<>(page, nodeVoList);
    }


}
